package com.zelin.web.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zelin.pojo.R;
import com.zelin.pojo.Student;
import com.zelin.service.StudentService;
import jdk.nashorn.internal.runtime.UserAccessorProperty;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/10/12-16:59
 * ------------------------------
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;
    @Override
    public void init() throws ServletException {
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
        studentService = wac.getBean(StudentService.class);
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 得到请求参数
        String cmd = req.getParameter("cmd");
        //2. 根据参数调用不同的方法
        if(StrUtil.isNotBlank(cmd)){
            if("list".equalsIgnoreCase(cmd)){
                list(req,resp);
            }else if("search".equalsIgnoreCase(cmd)){
                search(req,resp);
            }else if("add".equals(cmd)){
                add(req,resp);
            }else if("update".equals(cmd)){
                update(req,resp);
            }else if("delete".equals(cmd)){
                delete(req,resp);
            }
        }
    }
    //1. 查看学生列表
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.1 得到学生列表
        List<Student> students = studentService.findAll();
        //1.2 将学生列表转换为json送到页面
        resp.getWriter().println(JSON.toJSONString(students));
        resp.getWriter().flush();
    }
    //2. 条件查询带分页
    private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //2.1 得到当前页及每页大小
        String page = req.getParameter("page");
        String pageSize = req.getParameter("pageSize");
        //2.2 得到传入的查询参数
        String stud = req.getParameter("stud");
        //2.3 转换为学生对象
        Student student = JSON.parseObject(stud, Student.class);
        //2.4 开始查询
        PageInfo<Student> pageInfo = studentService.search(new Integer(page),new Integer(pageSize),student);

        //2.5 将学生列表转换为json送到页面
        resp.getWriter().println(JSON.toJSONString(pageInfo));
        resp.getWriter().flush();
    }
    //3. 添加学生
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //3.1 得到传入的学生信息字符串
        String stud = req.getParameter("stud");
        //3.2 转换为学生对象
        Student student = JSON.parseObject(stud,Student.class);
        //3.3 添加学生
        studentService.insert(student);
        //3.4 将学生列表转换为json送到页面
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();
    }
    //4. 修改学生
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //4.1 得到传入的学生信息字符串
        String stud = req.getParameter("stud");
        //4.2 转换为学生对象
        Student student = JSON.parseObject(stud,Student.class);
        //4.3 修改学生
        studentService.update(student);
        //4.4 将学生列表转换为json送到页面
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();
    }

    //5. 删除学生
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //3.1 得到传入的学生学号
        String sid = req.getParameter("sid");
        //3.2 根据学号来删除学生
        studentService.delete(sid);
        //3.4 将学生列表转换为json送到页面
        resp.getWriter().println(JSON.toJSONString(R.ok()));
        resp.getWriter().flush();
    }
}
